package com.xinqi.modules.exam.common.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.exam.common.domain.CourseContentEntity;
import com.xinqi.modules.exam.common.manager.CourseContentManager;
import com.xinqi.modules.exam.common.mapper.CourseContentMapper;
import com.xinqi.modules.exam.paper.domain.CourseExamPaperEntity;
import com.xinqi.modules.exam.paper.enums.ExamStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 课程内容表管理层实现
 *
 * @author: linkai
 * @date: 2023/07/26
 */
@Service
@RequiredArgsConstructor
public class CourseContentManagerImpl extends BaseServiceImpl<CourseContentMapper, CourseContentEntity> implements CourseContentManager {

    /**
     * 根据id查询 课程内容表数据
     */
    @Override
    public CourseContentEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id更新 课程内容表数据，重写
     */
    @Override
    public boolean updateById(CourseContentEntity entity) {
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    /**
     * 根据id删除 课程内容表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    /**
     * 同步课程内容
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncCourseContent(CourseExamPaperEntity examPaper) {
        CourseContentEntity courseContent = Optional.ofNullable(this.baseMapper.selectByContentId(examPaper.getId(), CourseContentTypeEnum.EXAM.getCode()))
            .orElseGet(() -> {
                CourseContentEntity temp = new CourseContentEntity();
                temp.setClassId(examPaper.getClassId());
                temp.setUserId(examPaper.getUserId());
                temp.setContentId(examPaper.getId());
                temp.setType(CourseContentTypeEnum.EXAM.getCode());
                return temp;
            });
        // 设置状态
        if (Objects.equals(examPaper.getStatus(), ExamStatusEnum.草稿)) {
            courseContent.setStatus(YesNoEnum.NO.ordinal());
        } else {
            courseContent.setStatus(YesNoEnum.YES.ordinal());
        }
        courseContent.setChapterId(examPaper.getChapterId());
        courseContent.setTitle(examPaper.getName());
        courseContent.setContent(examPaper.getContent());
        courseContent.setType(CourseContentTypeEnum.EXAM.getCode());
        courseContent.setClassId(examPaper.getClassId());
        courseContent.setReleaseTime(examPaper.getReleaseTime());
        courseContent.setStartTime(examPaper.getStartTime());
        courseContent.setEndTime(examPaper.getEndTime());
        saveOrUpdate(courseContent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByExamIds(List<Long> list) {
        LambdaQueryWrapper<CourseContentEntity> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.in(CourseContentEntity::getContentId, list);
        updateWrapper.eq(CourseContentEntity::getType, CourseContentTypeEnum.EXAM.getCode());
        return this.remove(updateWrapper);
    }

}
